﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.basicData;
using baseSet.importExport;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhDataAcess.dataAcess;
using baseSet;
using dhDataAcess.produceLinq;
using dhDataAcess.baseBLL;
using dhSystemBLL.orderData;

namespace dhsystem.produceForm
{
    public partial class FrmrrltkMasterAdd : Form
    {
        purchase.rltkDetailDataTable rltkDetailTable = new purchase.rltkDetailDataTable();
        purchase.rltkMasterDataTable rltkMasterTable = new purchase.rltkMasterDataTable();
        rltkDetailModel rltkDetailRow = new rltkDetailModel();
        rltkBLL rltk = new rltkBLL();
        stockListBLL stockList = new stockListBLL();
        deptBLL dept = new deptBLL();
        rlckBLL rlck = new rlckBLL();
        workerListBLL workerList = new workerListBLL();
        rltkMasterModel rltkMasterRow = new rltkMasterModel();
        BindingSource b2;
        string state;
        string stockName;
        public FrmrrltkMasterAdd(BindingSource b1, string _state, string _stockName)
        {
            InitializeComponent();
            state = _state;
            b2 = b1;
            stockName = _stockName;
        }
        //打开窗口时绑定数据
        private void FrmrrltkMasterAdd_Load(object sender, EventArgs e)
        {
            textstockName.Text = stockName;
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";
            bindingSourcedeptList.DataSource = dept.getdeptListAll();
            comboBoxdeptNum.DataSource = bindingSourcedeptList;
            comboBoxdeptNum.DisplayMember = "deptName";
            comboBoxdeptNum.ValueMember = "deptNum";
            bindingSourceworkerList.DataSource = workerList.getworkerListAll();
            comboBoxworkername.DataSource = bindingSourceworkerList;
            comboBoxworkername.DisplayMember = "name";
            comboBoxworkername.ValueMember = "workerNum";

            //bindingSourcestockList.DataSource = stockList.getstockListAll();
            //comboBoxstockName.DataSource = bindingSourcestockList;
            //comboBoxstockName.DisplayMember = "stockName";
            //comboBoxstockName.ValueMember = "stockName";
            if (state == "新增")
            {
                textbillNo.Text = "R" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("领料退库单号");
                bindingSourcerltkDetail.DataSource = rltk.getsrltkDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourcerltkDetail;

            }
            if (state == "编辑")
            {
                comboBoxdeptNum.DataBindings.Add("SelectedValue", b2, "deptName");
                comboBoxworkername.DataBindings.Add("SelectedValue", b2, "workername");
                textbillNo.DataBindings.Add("text", b2, "billNo");
                textorderNo.DataBindings.Add("text", b2, "orderNo");
                textbillnum.DataBindings.Add("text", b2, "billnum");
                dateTimebillDate.DataBindings.Add("value", b2, "billDate");
                textstockName.DataBindings.Add("text", b2, "stockname");
                textmark.DataBindings.Add("text", b2, "mark");
                bindingSourcerltkDetail.DataSource = rltk.getsrltkDetailAllBybillNo(textbillNo.Text);
                dGridDetail.DataSource = bindingSourcerltkDetail;
            }
            DcolumnSet.colSet(moduleInfo.rltkDetail, dGridDetail);
            dGridDetail.AllowUserToAddRows = false;
           
        }
        int i = 0;
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textbillStatus.Text == "结算")
            {
                MessageBox.Show("此订单已经结算，不能进行修改。如需修改请反结算");
                return;
            }
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        purchase.rltkMasterRow rltkRow = rltkMasterTable.NewrltkMasterRow();
                        dhDataAcess.dataAcess.purchase.rltkDetailDataTable ptable = (dhDataAcess.dataAcess.purchase.rltkDetailDataTable)bindingSourcerltkDetail.DataSource;
                        rltkRow.billNo = textbillNo.Text;
                        rltkRow.deptName = comboBoxdeptNum.SelectedValue.ToString();
                        rltkRow.workername = comboBoxworkername.SelectedValue.ToString();
                        rltkRow.billDate = dateTimebillDate.Value;
                        rltkRow.billStatus = "";
                        rltkRow.orderNo = textorderNo.Text;
                        rltkRow.billnum = textbillnum.Text;
                        rltkRow.stockName = textstockName.Text;
                        rltkRow.createDate = DateTime.Now;
                        rltkRow.createUser = GlobalInfo.UserName;
                        rltkRow.updateDate = DateTime.Now;
                        rltkRow.updateUser = GlobalInfo.UserName;
                        rltkRow.mark = textmark.Text;
                        rltkMasterTable.AddrltkMasterRow(rltkRow);
                        rltkMasterTable.AcceptChanges();
                        foreach (purchase.rltkDetailRow  a in ptable)
                        {
                            if (!a.IsfqtyNull())
                            {
                                a.fqty = a.fqty;
                            }
                            else { a.fqty = 0; }
                            var s = ptable.Where(x => x.matNum == a.matNum).ToList();
                            if (s.Count() > 1)
                            {
                                MessageBox.Show("存在两条相同产品名称的记录，不能保存");
                                return;
                            }
                            if (!a.IsqtyNull())
                            {
                                a.qty = a.qty;
                            }
                            else
                            {
                                DialogResult sresult = MessageBox.Show("数量为空是否保存！", "提示信息", MessageBoxButtons.YesNo);
                                if (sresult == DialogResult.Yes)
                                {
                                    a.qty = 0;
                                }
                                if (sresult == DialogResult.No)
                                {
                                    MessageBox.Show("保存已取消");
                                    return;
                                }
                            }
                        }
                        rltk.rltkInsert(rltkMasterTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcerltkMaster.DataSource = rltk.getrltkMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //删除子件
        private void buttDel_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //编辑子件
        private void buttedit_Click(object sender, EventArgs e)
        {
            FrmrltkDetailAdd rltkDetail = new FrmrltkDetailAdd(bindingSourcerltkDetail, "编辑", textbillNo.Text);
            rltkDetail.ShowDialog();
        }
        //添加子件
        private void buttAdd_Click(object sender, EventArgs e)
        {
            FrmrltkDetailAdd rltkDetail = new FrmrltkDetailAdd(bindingSourcerltkDetail, "新增", textbillNo.Text);
            rltkDetail.ShowDialog();
        }
        //选择领料部门
        private void buttuserselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "deptNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.deptList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxdeptNum.SelectedValue = productselect.pname;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string field;
            field = "workerNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.workerList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                comboBoxworkername.SelectedValue = productselect.pname;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string field;
            field = "billNo";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.rlckDetail , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textbillnum.Text = productselect.pname;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string field;
            field = "orderNo";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.orderMaster, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textorderNo.Text = productselect.pname;
            }
        }

        private void textbillnum_TextChanged(object sender, EventArgs e)
        {
            dGridDetail.DataSource = rlck.getsrlckDetailAllBybillNo(textbillnum.Text);
        }

        private void toolExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void toolSave_Click(object sender, EventArgs e)
        {
            if (textbillStatus.Text == "结算")
            {
                MessageBox.Show("此订单已经结算，不能进行修改。如需修改请反结算");
                return;
            }
            if (textbillNo.Text == "")
            {
                MessageBox.Show("请输入库单号");
            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存信息！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        if (i == 0 && state == "新增")
                        {
                            state = "新增";
                        }
                        else
                        {
                            state = "编辑";
                        }
                        i++;
                        purchase.rltkMasterRow rltkRow = rltkMasterTable.NewrltkMasterRow();
                        dhDataAcess.dataAcess.purchase.rltkDetailDataTable ptable = (dhDataAcess.dataAcess.purchase.rltkDetailDataTable)bindingSourcerltkDetail.DataSource;
                        rltkRow.billNo = textbillNo.Text;
                        rltkRow.deptName = comboBoxdeptNum.SelectedValue.ToString();
                        rltkRow.workername = comboBoxworkername.SelectedValue.ToString();
                        rltkRow.billDate = dateTimebillDate.Value;
                        rltkRow.billStatus = "";
                        rltkRow.orderNo = textorderNo.Text;
                        rltkRow.billnum = textbillnum.Text;
                        rltkRow.stockName = textstockName.Text;
                        rltkRow.createDate = DateTime.Now;
                        rltkRow.createUser = GlobalInfo.UserName;
                        rltkRow.updateDate = DateTime.Now;
                        rltkRow.updateUser = GlobalInfo.UserName;
                        rltkRow.mark = textmark.Text;
                        rltkMasterTable.AddrltkMasterRow(rltkRow);
                        rltkMasterTable.AcceptChanges();
                        foreach (purchase.rltkDetailRow a in ptable)
                        {
                            if (!a.IsfqtyNull())
                            {
                                a.fqty = a.fqty;
                            }
                            else { a.fqty = 0; }
                            var s = ptable.Where(x => x.matNum == a.matNum).ToList();
                            if (s.Count() > 1)
                            {
                                MessageBox.Show("存在两条相同产品名称的记录，不能保存");
                                return;
                            }
                            if (!a.IsqtyNull())
                            {
                                a.qty = a.qty;
                            }
                            else
                            {
                                DialogResult sresult = MessageBox.Show("数量为空是否保存！", "提示信息", MessageBoxButtons.YesNo);
                                if (sresult == DialogResult.Yes)
                                {
                                    a.qty = 0;
                                }
                                if (sresult == DialogResult.No)
                                {
                                    MessageBox.Show("保存已取消");
                                    return;
                                }
                            }
                        }
                        rltk.rltkInsert(rltkMasterTable, ptable, state, textbillNo.Text, GlobalInfo.Date);
                        ptable.AcceptChanges();
                        bindingSourcerltkMaster.DataSource = rltk.getrltkMasterAll();
                        MessageBox.Show("保存成功");
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

        private void toolDel_Click(object sender, EventArgs e)
        {
            try
            {
                string id = dGridDetail.Rows[dGridDetail.CurrentRow.Index].Cells[0].Value.ToString();
                DialogResult result = MessageBox.Show("是否删除：" + id + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    dGridDetail.Rows.Remove(dGridDetail.CurrentRow);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void toolAdd_Click(object sender, EventArgs e)
        {
            textbillNo.Text = "R" + GlobalInfo.Date + "-" + basicSql.getOrderSerail("领料退库单号");
            bindingSourcerltkDetail.DataSource = rltk.getsrltkDetailAllBybillNo(textbillNo.Text);
            dGridDetail.DataSource = bindingSourcerltkDetail;
            dateTimebillDate.Value = DateTime.Now;
        }
    }
}
